<?php

namespace app\admin\controller;

use think\Controller;
use app\admin\model\Login;
use think\Db;

class Index extends Controller
{

    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return view('index/index');
    }
   
    //处理加载前台用户列表
    public function userlist()
    {
        $user = new Login();

            $list = Db::name('user')->paginate(10);
            if($list){
            return view('user/index',[
                'list'=>$list
            ]);
        }else{
            return $this->error('查询出错请稍后重试!');
        }
    }
    //处理ajax无刷新更改状态
    public function change()
    {
        $data = input('post.');
        $row = db('user')->where('id', $data['code'])->update(['status' => $data['n']]);
        if ($row){
            echo 1;
        }else{
            echo 2;
        }

    }

    //显示用户详情
    public function showDetail()
    {
        $id = input('post.id');
        $user = new Login();
        $result = $user->getDetail($id);

        return json($result);

    }
    //查看分类列表
    public function showCate()
    {
        $user = new Login();
        $data = $user->getCate();

        if($data){
            return view('category/index',[
                'data'=>$data,
            ]);
        }else{
            return $this->error('查询出错请稍后重试!');
        }
    }

    //处理切换分类状态
    public function changecate()
    {
        $data = input('post.');
//        halt($data);
        $row = Db::table('hb_category')
                ->where('category_id',$data['id'])
                ->update(['is_active'=>$data['code']]);
        if ($row){
            return 1;
        }else{
            return false;
        }
    }

    public function delete()
    {
        $id = input('post.id');
        $row = db('category')->delete($id);
        if ($row){
            return 1;
        }else{
            return false;
        }
    }

    public function addCate()
    {
        parse_str(input('post.str'),$arr);
        $user = new Login();
        $row = $user->addC($arr);
        if($row){
            $arr['id']=$row;
            $this->assign('data',$arr);
            return view('Category/add');
        }else{
            return false;
        }

    }

    public function search()
    {
        $data = input('get.');
        $list = db('user')
            ->where("nickname like '%{$data['data']}%'")
            ->select();
        if($list){
            return view('user/search',[
                'list'=>$list,
            ]);
        }
    }
}
